package ui;

import java.awt.*;
import java.awt.event.*;

import mainScreen.MainScreen;

import sidebar.Sidebar;
import tools.Enums;
import fileIO.Options;

//TODO: Set up mnemonics for everything once the UI layout has stabilized.
public class MainWindow implements ActionListener {
	Window win;
	
	
	ImageButton ibtn_tab1;
	ImageButton ibtn_tab2;
	
	Sidebar sidebar;
	MainScreen mainScreen;
	
	int mainX, mainY, mainWidth, mainHeight; //dimensions for the MainScreen panel
	
	public MainWindow(Options options)
	{
		mainScreen = null;
		//TODO: import from skin
		int SIDEBAR_THICKNESS = 200;
		Enums.SideBarEdge sideBarEdge = Enums.SideBarEdge.RIGHT;
		
		
		fullScreen();			//make it go fullscreen
		win.setLayout(null);	//cause we hate layouts /grin		
		
		sidebar = new Sidebar(options, this); //note, sidebar is responsible for spawning the main screens
		//TODO: fix teh SIDEBAR WIDTH with skin option
		switch(sideBarEdge)
		{
		case RIGHT : 
			sidebar.setBounds(win.getWidth()-SIDEBAR_THICKNESS, 0, win.getWidth(), win.getHeight());
			mainX = 0;
			mainY = 0;
			mainWidth = win.getWidth() - SIDEBAR_THICKNESS;
			mainHeight = win.getHeight();
			break;
		case LEFT  : 
			sidebar.setBounds(0, 0, SIDEBAR_THICKNESS, win.getHeight());
			mainX = SIDEBAR_THICKNESS;
			mainY = 0;
			mainWidth = win.getWidth() - SIDEBAR_THICKNESS;
			mainHeight = win.getHeight();
			break;
		case TOP   : 
			sidebar.setBounds(0, 0, win.getWidth(), SIDEBAR_THICKNESS);
			mainX = 0;
			mainY = SIDEBAR_THICKNESS;
			mainWidth = win.getWidth();
			mainHeight = win.getHeight() - SIDEBAR_THICKNESS;
			break;
		case BOTTOM: 
			sidebar.setBounds(0, win.getHeight() - SIDEBAR_THICKNESS, win.getWidth(), win.getHeight());
			mainX = 0;
			mainY = 0;
			mainWidth = win.getWidth();
			mainHeight = win.getHeight() - SIDEBAR_THICKNESS;
			break;
		}
		sidebar.setBackground(Color.black);
		sidebar.setLayout(null);
		win.add(sidebar);
		sidebar.initializeEverything();
		
		
		
		
		//make a button that will exit the prog
/*		ImageButton ibtn = spawnButton ("button", "Exit", "Exit", this, panel1, 160, 260, options);
		ImageButton debug = spawnButton("button", "Debug Database", "DebugDB", this, panel1, 160, 230, options);
		
		//make some test buttons - true toggles
		ImageButton ibtn1 = spawnButton ("toggle", "we are", "", 	null, panel1, 20, 100, options);
		ImageButton ibtn2 = spawnButton ("toggle", "true", "", 		null, panel1, 20, 135, options);
		ImageButton ibtn3 = spawnButton ("toggle", "toggles", "", 	null, panel1, 20, 170, options);
		ImageButton ibtn4 = spawnButton ("toggle", "galore", "", 	null, panel1, 20, 205, options);
		
		//make some test buttons - tab buttons
		ibtn_tab1 = spawnButton ("tab", "tab 1", "TAB1TAB1TAB1", this, panel1, 10, 2, options);
		ibtn_tab2 = spawnButton ("tab", "tab 2", "TAB2TAB2TAB2", this, panel1, 115, 2, options);
		
		sidebar.repaint();
		panel1.repaint();
		win.pack();	 */
	}
	
	public void setMainScreen(MainScreen newMainScreen) 
	{
		if(mainScreen != null)
		{
			win.remove(mainScreen.getPanel());
		}
		mainScreen = newMainScreen;
		mainScreen.getPanel().setBounds(mainX, mainY, mainWidth, mainHeight);
		mainScreen.getPanel().setVisible(true);
		win.add(mainScreen.getPanel());
		repaintAll();
		win.pack();
		
	}
	
	public void repaintAll()
	{
		win.repaint();
		mainScreen.repaint();
		sidebar.repaintAll();
	}
	
	private void fullScreen()
	{
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gs = ge.getDefaultScreenDevice();
		// Create a window for full-screen mode; add a button to leave full-screen mode
		Frame frame = new Frame(gs.getDefaultConfiguration());
		win = new Window(frame);
		
		// Determine if full-screen mode is supported directly
		if (gs.isFullScreenSupported()) {
		    gs.setFullScreenWindow(win);
		} else {
		    //TODO: fake fullscreenness later I guess
		}
	}
	
	public void actionPerformed(ActionEvent evt) {
		//TODO: Make this a switch also
		
        if (evt.getActionCommand().equals("Exit"))
        {	
        	System.exit(0);	//convention sez 0 = no probs
        } else if (evt.getActionCommand().equals("TAB1TAB1TAB1")) {
        	ibtn_tab2.setToggledDown(false);
        } else if (evt.getActionCommand().equals("TAB2TAB2TAB2")) {
        	ibtn_tab1.setToggledDown(false);
        } else{
			System.err.println("Unhandled event in MainWindow.java: " + evt.getActionCommand());
		}
		win.repaint();
    }
	
	public void repaint()
	{
		win.repaint();
	}
	public static void main(String[] args) 
	{
		Options options = new Options(); //this will read in the main game option file
		new MainWindow(options); //would help if i knew what kind of options we want tho. Oh well, I'm sure something will come up
	}
}
